package cc.sjfx.sharedoc.services;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import cn.mandroid.frame.toast.MLog;
import cn.mandroid.frame.utils.ThreadUtils;

import com.bmob.BmobProFile;
import com.bmob.btp.callback.DownloadListener;

import cc.sjfx.sharedoc.utils.CacheUtil;
import cc.sjfx.sharedoc.utils.StrUtils;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class DownloadService extends Service {
	Context context;
	int count;
	int len = 0;
	int index = 0;
	boolean downloading = true;
	Map<String, String> map;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		context = this;
		count = 0;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		len = CacheUtil.taskList.size();
		download();
		return super.onStartCommand(intent, flags, startId);
	}

	private void download() {
		// TODO Auto-generated method stub
		map = CacheUtil.taskList.get(index++);
		ThreadUtils.executorService.execute(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message message = new Message();
				message.what = 1;
				message.obj = map;
				handler.sendMessage(message);
			}
		});
	}

	private DownloadListener listener = new DownloadListener() {
		@Override
		public void onError(int arg0, String arg1) {
			// TODO Auto-generated method stub
			count++;
			checkIsFinished();
		}

		@Override
		public void onSuccess(String arg0) {
			// TODO Auto-generated method stub
			File file=new File(arg0);
			File newFile=new File(StrUtils.DOWNLOADPATH);
			if(!newFile.exists()){
				newFile.mkdirs();
			}
			newFile=new File(StrUtils.DOWNLOADPATH+"/"+map.get("fileName"));
			file.renameTo(newFile);
			count++;
			checkIsFinished();
		}

		@Override
		public void onProgress(String arg0, int arg1) {
			// TODO Auto-generated method stub
			len = CacheUtil.taskList.size();
			Intent intent=new Intent("downloadFile");
			intent.putExtra("progress", arg1);
			intent.putExtra("message", "正在下载第"+index+"/"+len+"个文件");
			context.sendBroadcast(intent);
		}
	};
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				MLog.i("来了");
				Map<String, String> map = (Map<String, String>) msg.obj;
				BmobProFile.getInstance(context).download(
						map.get("fileNetName"), listener);
				break;
			default:
				break;
			}
		}
	};

	protected void checkIsFinished() {
		// TODO Auto-generated method stub
		len = CacheUtil.taskList.size();
		if (count >= len) {
			MLog.i("下载完成" + len);
			Intent intent=new Intent("finishDownload");
			context.sendBroadcast(intent);
			CacheUtil.taskList = new ArrayList<Map<String, String>>();
			stopSelf();
		} else {
			download();

		}
	}
}
